package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IPermissionService;
import cn.wolfcode.rbac.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("role")
public class RoleController {

    @Autowired
    IRoleService roleService;
    @Autowired
    IPermissionService permissionService;

    @RequestMapping("/list")
    public String list(QueryObject qo, Model model) {
        model.addAttribute("result", roleService.query(qo));
        return "role/list";
    }

    @RequestMapping("input")
    public String input(Model model, Long id) {
        if (id != null) {
            Role role = roleService.get(id);
            model.addAttribute("entity", role);
        }
        model.addAttribute("permissions", permissionService.listAll());
        return "role/input";
    }

    @RequestMapping("saveOrUpdate")
    public String saveOrUpdate(Role role, Long[] ids) {
        roleService.saveOrUpdate(role, ids);
        return "redirect:/role/list.do";
    }

    @RequestMapping("/delete")
    public String delete(Long id) {
        roleService.delete(id);
        return "redirect:/role/list.do";
    }
}
